/**
 * Created by Dalyn on 2017/5/8.
 */
import React from "react";
import {hashHistory} from 'react-router';
import Relay from "react-relay";
import { Table,Button, Icon, Popconfirm, message, Input, Modal } from 'antd';
import ReactKey from "../../../../util/ReactKey";
import ProductionNumberDetail from '../mutation/ProductionNumberMutation/ProductNumberListMutation';
import "../css/productionLicense_index.css";
import "../css/productionNumberDetail.css";
import "../../css/productionManagement_common.css";

const { Column, ColumnGroup } = Table;
/*导出组件*/
export default class ProductionColour extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
          dataSource: [],
          proNumIds: [],
          context:{},
          imgLook: null,
          visible: false
        };
    }

    componentWillMount(){
      this.getProductNumberList()
    }
    //获取数据列表
    getProductNumberList() {
      Relay.Store.commitUpdate(
          new ProductionNumberDetail({
            pdId: this.props.pdId
          }),
          {
              onFailure: () => {
                  message.error("网络请求失败")
              },
              onSuccess: (response) => {
                 let errorCode = response.getProNumDetailList.responseData.errorCode
                 if (errorCode == 0) {
                   let proNumDetailModelList = response.getProNumDetailList.responseData.proNumDetailModelList
                   proNumDetailModelList.push({sum:1})
                   this.setState({
                     dataSource: proNumDetailModelList
                   })
                 } else if(errorCode == 500){
                   message.error('服务器错误，请联系系统管理员')
                 }
              }
          }
      );
    }

    //合同数总和
    numberSum(type) {
      let numberSum = 0
      this.state.dataSource.map((v, i, ele)=>{
        if(type == 'addXS' && v.addXS != undefined) {
          numberSum+=Number(v.addXS)
          return numberSum
        } else  if(type == 'addS' && v.addS != undefined) {
          numberSum+=Number(v.addS)
          return numberSum
        } else  if(type == 'addM' && v.addM != undefined) {
          numberSum+=Number(v.addM)
          return numberSum
        } else  if(type == 'addL' && v.addL != undefined) {
          numberSum+=Number(v.addL)
          return numberSum
        } else  if(type == 'addXL' && v.addXL != undefined) {
          numberSum+=Number(v.addXL)
          return numberSum
        } else  if(type == 'add2XL' && v.add2XL != undefined) {
          numberSum+=Number(v.add2XL)
          return numberSum
        } else  if(type == 'add3XL' && v.add3XL != undefined) {
          numberSum+=Number(v.add3XL)
          return numberSum
        } else  if(type == 'add4XL' && v.add4XL != undefined) {
          numberSum+=Number(v.add4XL)
          return numberSum
        } else  if(type == 'add5XL' && v.add5XL != undefined) {
          numberSum+=Number(v.add5XL)
          return numberSum
        } else  if(type == 'compactXS' && v.compactXS != undefined) {
          numberSum+=Number(v.compactXS)
          return numberSum
        } else  if(type == 'compactS' && v.compactS != undefined) {
          numberSum+=Number(v.compactS)
          return numberSum
        } else  if(type == 'compactM' && v.compactM != undefined) {
          numberSum+=Number(v.compactM)
          return numberSum
        } else  if(type == 'compactL' && v.compactL != undefined) {
          numberSum+=Number(v.compactL)
          return numberSum
        } else  if(type == 'compactXL' && v.compactXL != undefined) {
          numberSum+=Number(v.compactXL)
          return numberSum
        } else  if(type == 'compact2XL' && v.compact2XL != undefined) {
          numberSum+=Number(v.compact2XL)
          return numberSum
        } else  if(type == 'compact3XL' && v.compact3XL != undefined) {
          numberSum+=Number(v.compact3XL)
          return numberSum
        } else  if(type == 'compact4XL' && v.compact4XL != undefined) {
          numberSum+=Number(v.compact4XL)
          return numberSum
        } else  if(type == 'compact5XL' && v.compact5XL != undefined) {
          numberSum+=Number(v.compact5XL)
          return numberSum
        } else if(type == 'itemTotal' && v.itemTotal != undefined) {
          numberSum+=Number(v.itemTotal)
          return numberSum
        }
      })
      return numberSum
    }
    imgLook(imgURL) {
      if (imgURL) {
        this.setState({
          visible: true,
          imgLook: imgURL
        })
      }
    }
    afterClose(){
      this.setState({
        imgLook: null
      })
    }
    showModal = () => {
       this.setState({
         visible: true,
       });
     }
     handleOk = (e) => {
       console.log(e);
       this.setState({
         visible: false,
       });
     }
     handleCancel = (e) => {
       console.log(e);
       this.setState({
         visible: false,
       });
     }
    render(){
        return (
            <div className="poduction_manage_tab_box" >
                <Table
                  bordered
                  scroll={{ x: 3100  }}
                  pagination={false}
                  rowKey={record => record.numberDetailId}
                  className='order_table'
                  dataSource = {this.state.dataSource}>
                   <Column
                    title="颜色"
                    dataIndex="colour"
                    key="colour"
                    width='100px'
                    fixed='left'
                    render= {(text, record, index) => {
                      if (record.sum == 1) {
                        return ''
                      } else {
                        return record.colour
                      }
                    }}
                  />
                   <Column
                    title="图片"
                    dataIndex="imgURL"
                    key="imgURL"
                    width='100px'
                    fixed='left'
                    render= {(text, record, index) => {
                      if (record.sum == 1) {
                        return '合计'
                      } else if (record.imgURL != null){
                         return (<img width="94" onClick={this.imgLook.bind(this,record.imgURL)} className="order_from_td_img_context" alt="" src={record.imgURL}/>)
                      }
                    }}
                  />
                   <ColumnGroup title="XS">
                     <Column
                       title="合同数"
                       dataIndex="addXS"
                       key="addXS"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('addXS')
                         } else {
                           return record.addXS
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compactXS"
                       key="compactXS"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactXS')
                         } else {
                            return record.compactXS
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactXS')+this.numberSum('addXS')
                         } else {
                           return Number(record.totalXS)  || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="S">
                     <Column
                       title="合同数"
                       dataIndex="addS"
                       key="addS"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('addS')
                         } else {
                            return record.addS
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compactS"
                       key="compactS"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactS')
                         } else {
                            return record.compactS
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactS')+this.numberSum('addS')
                         } else {
                             return Number(record.totalS) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="M">
                     <Column
                       title="合同数"
                       dataIndex="addM"
                       key="addM"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('addM')
                         } else {
                            return record.addM
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compactM"
                       key="compactM"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactM')
                         } else {
                            return record.compactM
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactM')+this.numberSum('addM')
                         } else {
                             return Number(record.totalM) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="L">
                     <Column
                       title="合同数"
                       dataIndex="addL"
                       key="addL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('addL')
                         } else {
                            return record.addL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compactL"
                       key="compactL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactL')
                         } else {
                            return record.compactL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactL')+this.numberSum('addL')
                         } else {
                           return Number(record.totalL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="XL">
                     <Column
                       title="合同数"
                       dataIndex="addXL"
                       key="addXL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('addXL')
                         } else {
                            return record.addXL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compactXL"
                       key="compactXL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactXL')
                         } else {
                            return record.compactXL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compactXL')+this.numberSum('addXL')
                         } else {
                           return Number(record.totalXL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="2XL">
                     <Column
                       title="合同数"
                       dataIndex="add2XL"
                       key="add2XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('add2XL')
                         } else {
                            return record.add2XL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compact2XL"
                       key="compact2XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact2XL')
                         } else {
                            return record.compact2XL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact2XL')+this.numberSum('add2XL')
                         } else {
                           return Number(record.total2XL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="3XL">
                     <Column
                       title="合同数"
                       dataIndex="add3XL"
                       key="add3XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('add3XL')
                         } else {
                            return record.add3XL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compact3XL"
                       key="compact3XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact3XL')
                         } else {
                            return record.compact3XL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact3XL')+this.numberSum('add3XL')
                         } else {
                          return Number(record.total3XL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="4XL">
                     <Column
                       title="合同数"
                       dataIndex="add4XL"
                       key="add4XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('add4XL')
                         } else {
                            return record.add4XL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compact4XL"
                       key="compact4XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact4XL')
                         } else {
                            return record.compact4XL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact4XL')+this.numberSum('add4XL')
                         } else {
                           return Number(record.total4XL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <ColumnGroup title="5XL">
                     <Column
                       title="合同数"
                       dataIndex="add5XL"
                       key="add5XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('add5XL')
                         } else {
                            return record.add5XL
                         }
                       }}
                     />
                     <Column
                       title="加裁数"
                       dataIndex="compact5XL"
                       key="compact5XL"
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact5XL')
                         } else {
                            return record.compact5XL
                         }
                       }}
                     />
                     <Column
                       title="裁剪数"
                       dataIndex=""
                       key=""
                       width='100px'
                       render= {(text, record, index) => {
                         if(record.sum == 1) {
                           return this.numberSum('compact5XL')+this.numberSum('add5XL')
                         } else {
                           return Number(record.total5XL) || 0
                         }
                       }}
                     />
                   </ColumnGroup>
                   <Column
                     title="裁剪总数"
                     dataIndex="age"
                     key="age"
                     width='100px'
                     fixed= 'right'
                     render= {(text, record, index) => {
                       if (record.sum == 1) {
                         return this.numberSum('itemTotal')
                       } else {
                         return record.itemTotal
                       }
                     }}
                   />
                   <Column
                     title="备注"
                     dataIndex="remark"
                     key="remark"
                     width='100px'
                     fixed= 'right'
                     render= {(text, record, index) => {
                       if (record.sum == 1) {
                         return ''
                       } else {
                         return record.remark
                       }
                     }}
                   />
                 </Table>
                  <Modal title="查看图片" visible={this.state.visible}
                    onOk={this.handleOk} onCancel={this.handleCancel}
                    footer={null}
                    wrapClassName='number_detail_img_modal'
                  >
                    <img src={this.state.imgLook} alt="图片" />
                  </Modal>
            </div>
        )
    }
}
